<?php
	
	namespace Aspic\Log;
	
	class FileLoggerObserver implements \SplObserver {
		const ST_PRINT_R = 'stPrintR';

		/**
		* A readable text (seprate with line of - and blank line)
		*/
		const ST_TEXT = 'stText';
		
		/**
		* Each line will contains string get
		*/
		const ST_STRING = 'stString';
		const ST_SERIALIZE = 'stSerialize';
	
		protected $filePath;
		protected $storageType;
		
		public function __construct($filePath,$storageType = null) {
			if(is_null($storageType))
				$storageType = self::ST_PRINT_R;
			
			$this->filePath = $filePath;
			$this->storageType = $storageType;
		}
		
		public function update(\SplSubject $subject) {
			$data = $subject->getLogData();
		
			$file = new File\File($this->filePath);
			
			if($file->open('a', LOCK_EX)) {
				if($this->storageType == self::ST_PRINT_R) {
					$file->puts(print_r($data,true)."\n".str_repeat('-',90)."\n");
				}
				elseif($this->storageType == self::ST_TEXT) {
					$file->puts($data."\n".str_repeat('-',90)."\n");
				}
				elseif($this->storageType == self::ST_STRING) {
					$file->puts($data."\n");
				}
				elseif($this->storageType == self::ST_SERIALIZE) {
					$file->puts(serialize($data)."\n");
				}
				
				$file->close();
				
				return true;
			}
			else {
				return false;
			}
			
		}
	}	
	
?>